import router from './router'
import store from './store'
// 进度条
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
const whiteList = ['/login' || '/404']
NProgress.start()
router.beforeEach(async(to, from, next) => {
  //   console.log(to)
  document.title = getPageTitle(to.meta.title)
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // 获取有token获取用户信息
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/userDataInfo')
      }
      next()
    }
  } else {
    // 没有token去白名单
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
